//
//  YSLBuyViewController.m
//  used
//
//  Created by lanou on 16/7/26.
//  Copyright © 2016年 王凯. All rights reserved.
//

#import "YSLBuyViewController.h"

@interface YSLBuyViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *userimge; //头像

@property (weak, nonatomic) IBOutlet UILabel *price; //实付金额

@property (weak, nonatomic) IBOutlet UILabel *saleprice; //现价

@property (weak, nonatomic) IBOutlet UILabel *desc; //商品信息

@property (weak, nonatomic) IBOutlet UILabel *city; //城市名字

@property (weak, nonatomic) IBOutlet UILabel *area; //城市区域

@end

@implementation YSLBuyViewController

- (UIImageView *)imageView {

    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
    }

    return _imageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"购买宝贝";
    self.view.backgroundColor = KLRGBColor(240, 240, 240);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(leftAction:)];
    self.userimge.image = self.imageView.image;
    self.price.text = self.pric;
    self.saleprice.text = self.salepric;
    self.desc.text = self.des;
    self.city.text = self.cit;
    self.area.text = self.are;
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)leftAction:(UIBarButtonItem *) sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}



//支付
- (IBAction)sureButton:(id)sender {
    
    NSString *partner = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"Partner"];
    NSString *seller = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"Seller"];
    NSString *privateKey = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"RSA private key"];
    /*============================================================================*/
    /*============================================================================*/
    /*============================================================================*/
    
    //partner和seller获取失败,提示
    if ([partner length] == 0 ||
        [seller length] == 0 ||
        [privateKey length] == 0){
        
        NSLog(@"缺少partner或者seller或者私钥。");
        return;
    }
    
    /*
     *生成订单信息及签名
     */
    //将商品信息赋予AlixPayOrder的成员变量
    Order *order = [[Order alloc] init];
    order.partner = partner;
    order.seller = seller;
    order.tradeNO = [NSString stringWithFormat:@"%ld",(arc4random()%111111111111) + 10000000001]; //订单ID（由商家自行制定）
    
/////////////////////////////////////////////////////////////
    //商品标题
    order.productName = @"ysl";
    //商品描述
    order.productDescription = self.desc.text;
    //商品价格
    order.amount = self.price.text;
    
    NSLog(@"chenglianshiye@yeah.net");
//#pragma mark 疑问1.
//    order.notifyURL =  self.notifityURL.text; //回调URL
    
    //以下配置信息是默认信息,不需要更改.
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showUrl = @"m.alipay.com";
    
    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types,用于快捷支付成功后重新唤起商户应用
    NSString *appScheme = @"AliPayTest";
    
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    NSLog(@"orderSpec = %@",orderSpec);
    
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = [signer signString:orderSpec];
    
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = nil;
    if (signedString != nil) {
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec, signedString, @"RSA"];
        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            NSLog(@"reslut = %@",resultDic);
        }];
        
    }}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
